package org.dropme.model;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.http.params.HttpParams;
import org.json.JSONObject;

public class Litter {

	private Long key;
	private String title;
	private String description;
	private Date date;
	private String user;
	private Double latitude;
	private Double longitude;
	private Long geoTag;
	
	public Litter(String title, String description, Date date, String user,
			Double latitude, Double longitude) {
		super();
		this.title = title;
		this.description = description;
		this.date = date;
		this.user = user;
		this.latitude = latitude;
		this.longitude = longitude;
	}

	public Long getKey() {
		return key;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public Date getDate() {
		return date;
	}

	public void setDate(Date date) {
		this.date = date;
	}

	public String getUser() {
		return user;
	}

	public void setUser(String user) {
		this.user = user;
	}

	public Double getLatitude() {
		return latitude;
	}

	public void setLatitude(Double latitude) {
		this.latitude = latitude;
	}

	public Double getLongitude() {
		return longitude;
	}

	public void setLongitude(Double longitude) {
		this.longitude = longitude;
	}
	
	public void setGeoTag(Double latitude, Double longitude) {
		geoTag = createGeoTag(latitude, longitude);
	}
	
	/**
	 * 
	 * Creates a geo tag for storing the location in the map grid.
	 * 
	 * @param latitude
	 * @param longitude
	 * @return
	 */
	public static Long createGeoTag(Double latitude, Double longitude) {
		Long lat = Math.round(latitude * 1e3);
		Long lon = Math.round(longitude * 1e3);
		return (long) (lat + (lon * 1e6));
	}
	
	public Long getGeoTag() {
		return geoTag;
	}
	
	private static final SimpleDateFormat format = new SimpleDateFormat();
	
	public static Litter fromJSON(JSONObject object) {
		try {
			String title = object.getString("title");
			String description = object.getString("description");
			String dateString = object.getString("date");
			Date date = format.parse(dateString);
			String user = object.getString("user");
			Double latitude = object.getDouble("latitude");
			Double longitude = object.getDouble("longitude");
			return new Litter(title, description, date, user, latitude, longitude);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public HttpParams attach(HttpParams params) {
		params.setParameter("title", title);
		params.setParameter("description", description);
		params.setParameter("user", user);
		params.setParameter("date", date.toString());
		params.setDoubleParameter("latitude", latitude);
		params.setDoubleParameter("longitude", longitude);
		return params;
	}
	
	public JSONObject toJSON() {
		Map<String, String> map = new HashMap<String, String>();
		map.put("key", key.toString());
		map.put("title", title);
		map.put("user", user);
		map.put("description", description);
		map.put("date", date.toString());
		map.put("latitude", latitude.toString());
		map.put("longitude", longitude.toString());
		return new JSONObject(map);
	}

}
